package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 21:50
 * https://leetcode-cn.com/problems/multiply-strings/
 */
public class Title43 {

    public static String multiply(String num1, String num2) {
        if ("0".equals(num1) || "0".equals(num2)) {
            return "0";
        }
        //456 * 123
        int[] result = new int[num1.length() + num2.length()];
        //123
        for (int i = num1.length() - 1; i >= 0; i--) {
            int x = num1.charAt(i) - '0';
            //456
            for (int j = num2.length() - 1; j >= 0; j--) {
                int y = num2.charAt(j) - '0';
                //放到末尾
                result[i + j + 1] += x * y;
            }
        }
        for (int i = result.length - 2; i >= 0; i--) {
            result[i] += result[i + 1] / 10;
            result[i + 1] %= 10;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < result.length; i++) {
            if (result[i] == 0 && sb.length() == 0) {
                continue;
            }
            sb.append(result[i]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        multiply("0", "0");
    }
}
